package com.example.newbiechen.ireader.service.boluoxs;

import android.util.Log;

import com.example.newbiechen.ireader.model.bean.CollBookBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by robrk on 2019/3/16.
 */

public class BookDetailService {
    private String url;
    private DownloadFinish downloadFinish;
    private boolean isFinish;

    public boolean isFinish() {
        return isFinish;
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }

    public BookDetailService(String url, DownloadFinish downloadFinish) {
//        if(url == null || url.length() == 0){
//            this.url = "https://m.boluoxs.com/book/" + id + ".html";
//        }else {
//            this.url = url;
//        }
        this.url = url;
        this.downloadFinish = downloadFinish;
    }

    public void download(){
        this.isFinish = false;
        this.getByUrl(this.url);
    }

    private void onSuccess(CollBookBean collBookBean){
        if(this.downloadFinish != null){
            this.downloadFinish.onFinish(collBookBean);
        }
        this.setFinish(true);
    }

    public static CollBookBean getByUrlAsync(String url) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
//        Log.d("BookDetailService", "run: " + response.body().string());
        byte[] b = response.body().bytes(); //获取数据的bytes
        String html = new String(b, "GBK"); //然后将其转为gbk
        System.out.println(html);
        CollBookBean collBookBean = HtmlHelper.getBookDetail(html);
        return collBookBean;
    }

    private void getByUrl(String url){
//        String url = "https://m.boluoxs.com" + urlPath;
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new BookDetailCallBack(this));
    }

    private class BookDetailCallBack implements Callback {
        private BookDetailService bookDetailService;

        public BookDetailService getBookDetailService() {
            return bookDetailService;
        }

        public BookDetailCallBack(BookDetailService bookDetailService) {
            this.bookDetailService = bookDetailService;
        }

        @Override
        public void onFailure(Call call, IOException e) {
            this.getBookDetailService().setFinish(true);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            byte[] b = response.body().bytes(); //获取数据的bytes
            String html = new String(b, "GBK"); //然后将其转为gbk
            System.out.println(html);
            CollBookBean collBookBean = HtmlHelper.getBookDetail(html);
//            collBookBean.setBookId();
//            this.getBookDetailService().setFinish(true);
            this.getBookDetailService().onSuccess(collBookBean);
        }
    }
}
